package com.yj.sulotion;

import java.util.Arrays;

public class TwoSum {
    public static void main(String[] args) {

        int [] arr = {3,3};
        int[] sum = toSum2(arr, 6);
        System.out.println(Arrays.toString(sum));
    }

    private static int[] toSum1(int[] arr, int target) {
        int [] result = new int[2];
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length ; j++) {
                if ((arr[i]+arr[j]) == target){
                    result[0] = i;
                    result[1] = j;
                }
            }
        }
        return result;


    }
    private static int[] toSum2(int[] arr, int target) {
        int [] result = new int[2];
        int index = arr.length-1;
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i]+arr[index]) == target && i<index){
                result[0] = i;
                result[1] = index;
            } else {
                index--;
            }
        }
        return result;


    }

}
